<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>顺子牌</title>
</head>
<body>
  <script>
    function fn(hand, W) {
      if (hand.length % W != 0) return false
      if (W == 1) return true
      var hand = hand.sort((a, b) => a - b)
      for (var i = 0; i < hand.length; i++) {
        var flag = 0
        for (var j = 1; j < W; j++) {
          var a = hand[i] + j
          if (hand.indexOf(a) != -1) {
            hand.splice(hand.indexOf(a), 1)
            flag = true
          }
        }
        if (flag) {
          hand.splice(i, 1)
          i--
        } else {
          return false
        }
      }
      return true
    }
    console.log(fn([1, 2, 3, 6, 2, 3, 4, 7, 8], 3))
  </script>
</body>
</html>